package mx.com.crowdgine.assetloader;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GL4bc;
import javax.media.opengl.glu.GLU;

import mx.com.crowdgine.util.ResourceFinder.ResourceRetriever;
import mx.com.crowdgine.util.math.MathCommons;

//import com.sun.opengl.util.BufferUtil;


public class RawTexture extends Texture{
	
	private int textures[] = new int[1]; //1, pero bien puede ser cualquier otro numero, es un id para las texturas que se almacenaran.
	private int format = 4; //Default is 4 to represent RGBA
	
	public RawTexture() {
		;
	}
	
	public RawTexture(int width, int height, int format, String fileName){
		super(width, height);
		this.format =format;
		this.data = ByteBuffer.allocateDirect(width * height * format);
		this.data.position(0);
		this.data.limit(this.data.capacity());
	}
	
	public void readTextureData(String filename, TextureImage buffer) throws IOException{
		int i,j,k,done=0;	
		int stride = buffer.width * buffer.format; //Size of a Row.
		InputStream is = ResourceRetriever.getResourceAsStream(filename);
		 // Loop Through Height (Bottoms Up - Flip Image)
        for (i = buffer.height - 1; i >= 0; i--) { 
            int p = i * stride;          
            for (j = 0; j < buffer.width; j++) {  // Loop Through Width
                for (k = 0; k < buffer.format - 1; k++, p++, done++) {
        // Read Value From File And Store In Memory
                    buffer.data.put(p, (byte) is.read());  
                }
                buffer.data.put(p, (byte) 255);
                p++;  // Store 255 In Alpha Channel And Increase Pointer
            }
        }
        is.close();      // Close The File
	}
	
	public void buildTexture(GL4bc gl, GLU glu){
		gl.glGenTextures(1,textures,0);
		gl.glBindTexture(GL.GL_TEXTURE_2D,textures[0]);
		gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
		gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
	//	glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, GL.GL_RGB, this.width, this.height, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, this.data);	
	}
	
	public void blitter(TextureImage other, int src_xstart, int src_ystart, int src_width, int src_height, int dst_xstart, int dst_ystart, boolean blend, int alpha){
		int s,d;
		
		//Clamp alpha value only when out of range
		alpha=MathCommons.clamp(alpha, 0, 255);
		
		d = (dst_ystart * other.width * other.format);
		s = (src_ystart * this.width * this.format);
		
		for(int i = 0; i < src_height; i++){
			s = s + (src_xstart * this.format);
			d = d + (dst_xstart* other.format);
			
			for(int j=0; j<src_width; j++){
				for(int k=0; k < this.format; k++,d++,s++){
					if(blend){
						other.data.put(d, (byte) (((this.data.get(s) * alpha) + (other.data.get(d) * (255 - alpha))) >> 8));
					}
					else{
						other.data.put(d, this.data.get(s));
					}
				}
			}
			
			d= d + (other.width - (src_width + dst_xstart)) * other.format;
			s= s + (this.width - (src_width + src_xstart)) * this.format;
		}	
	}

	public int getFormat() {
		return format;
	}

	
}
